﻿using AutoMapper;
using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.PackageTypeCommand;
using MediatR;
using NLog.Time;

namespace D.Medicalcare.Read.API.Application.Handler.PackageTypeHandler
{
    /// <summary>
    /// 套餐类型查询接口实现类
    /// </summary>
    public class PackageTypeHandler : IRequestHandler<PackageTypeCommand, APIPaging<PackageTypeModel>>
    {

        private readonly IBaseRepository<PackageTypeModel> baseRepository;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository">仓储</param>
        /// <param name="mapper">映射</param>
        public PackageTypeHandler(IBaseRepository<PackageTypeModel> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 套餐类型查询接口实现方法
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">参数</param>
        /// <returns></returns>
        public async Task<APIPaging<PackageTypeModel>> Handle(PackageTypeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<PackageTypeModel> result = new APIPaging<PackageTypeModel>();
                result.Code = APIEnum.Mvc;
                result.Msg = "查询成功";
                var list = baseRepository.GetAll().Where(x=>x.IsDeleted==false).ToList();

                result.TotalCount = list.Count();
                result.PageCount = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                result.Data = list.OrderByDescending(a => a.PackageTypeId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                return await Task.FromResult(result);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
